package com.wln.pms.pms.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wln.pms.pms.conf.Md5Util;
import com.wln.pms.pms.conf.OssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wln.pms.pms.entity.UserEntity;
import com.wln.pms.pms.service.UserService;
import com.wln.common.utils.PageUtils;
import com.wln.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author wulinna
 * @email 2258732595@qq.com
 * @date 2022-02-22 14:27:49
 */
@RestController
@RequestMapping("pms/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private OssUtils ossUtils;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    public R info(@PathVariable("userId") Long userId) {
        UserEntity user = userService.getById(userId);

        return R.ok().put("user", user);
    }

    /**
     * 上传图片
     */
    @RequestMapping("/upload")
    public R upload(MultipartFile file) {
        R r = ossUtils.uploadFile(file);
        return r;
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user) {
        userService.save(user);

        return R.ok();
    }

    /**
     * 注册
     */
    @RequestMapping("/register")
    public R register(@RequestBody UserEntity user) {
        UserEntity userName = userService.getOne(new QueryWrapper<UserEntity>().eq("user_name", user.getUserName()));
        UserEntity userId = userService.getOne(new QueryWrapper<UserEntity>().eq("user_id", user.getUserId()));
        if (userId != null || userName != null) {
            return R.error("该用户已注册，请登录");
        } else {
            String salt = "1a2b3c";
            user.setUserPwd(Md5Util.md5Salt(Md5Util.md5Salt(user.getUserPwd(), salt), salt));
            user.setSalt(salt);
            user.setRegisterDate(new Date());
            user.setLoginCount(0);
            userService.save(user);
            return R.ok();
        }
    }

    /**
     * 注册
     */
    @RequestMapping("/login")
    public R login(@RequestBody UserEntity user) {
        String salt = "1a2b3c";
        user.setUserPwd(Md5Util.md5Salt(Md5Util.md5Salt(user.getUserPwd(), salt), salt));
        UserEntity user_name = userService.getOne(new QueryWrapper<UserEntity>().eq("user_name", user.getUserName()));
        UserEntity user_id = userService.getById(user.getUserName());
        if(user_name!=null){
            if(user_name.getUserPwd().equals(user.getUserPwd())){
                user_name.setLoginCount(user_name.getLoginCount()+1);
                user_name.setLastLoginDate(new Date());
                userService.updateById(user_name);
                return R.ok("登录成功").put("user",user_name);
            }
        }else if(user_id!=null){
            if(user_id.getUserPwd().equals(user.getUserPwd())){
                user_id.setLoginCount(user_id.getLoginCount()+1);
                user_id.setLastLoginDate(new Date());
                userService.updateById(user_id);
                return  R.ok("登录成功").put("user",user_id);
            }
        }
        return R.error("登录失败");
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user) {
        userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] userIds) {
        userService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

}
